<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
       /* $list = \think\Db::name('paidan')
            //->where('user_id', session('user.id')) // 只显示当前用户的订单
            ->order('createtime', 'desc')
            ->paginate(10);

        $this->assign('list', $list);
        $this->assign('page', $list->render());*/
        $this->redirect('/root.php');
        return $this->view->fetch();
    }

    public function accept()
    {
        $id = $this->request->post('id');
        $order = \think\Db::name('paidan')->where('id', $id)->find();

        if (!$order) {
            return json(['code' => 0, 'msg' => '订单不存在']);
        }

        if ($order['state'] == '1') {
            return json(['code' => 0, 'msg' => '该订单已被接单']);
        }

        $result = \think\Db::name('fa_paidan')
            ->where('id', $id)
            ->update([
                'state' => '1',
                'user_id' => session('user.id'),
                'updatetime' => time()
            ]);

        if ($result) {
            return json(['code' => 1, 'msg' => '接单成功']);
        } else {
            return json(['code' => 0, 'msg' => '接单失败']);
        }
    }

}
